import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'package:my_flutter_github/common/db/SqlManager.dart';

///数据库数据提供的抽象基类
abstract class BaseDbProvider {
  bool isTableExits = false;
  tableSqlString();
  tableName();
  tableBaseString(String name, String columnId){
    return
      '''
       create table $name (
        $columnId integer primary key autoincrement,
      ''';
  }

  @mustCallSuper
  prepare(String tableName, String createSql) async {
      isTableExits = await SqlManager.isTableExits(tableName);
      if(!isTableExits) {
        var database = await SqlManager.getCurrentDatabase();
        return await database.execute(createSql);
      }
  }

  @mustCallSuper
  open() async {
    if(!isTableExits) {
      await prepare(tableName(), tableSqlString());
    }
    return await SqlManager.getCurrentDatabase();
  }

  Future<Database> getDatabase() async {
    return await open();
  }
}